package com.example.demoback.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demoback.common.Result;
import com.example.demoback.entity.Serious;
import com.example.demoback.entity.User;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import com.example.demoback.service.IRoleService;
import com.example.demoback.entity.Role;


import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yyd
 * @since 2022-06-28
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    //引入Service
    @Resource
    private IRoleService roleService;

    @PostMapping("/save")
    public Boolean save(@RequestBody Role role) {
        //新增或更新
        Boolean flag = false;
        int a ;
        a = getInfo(role.getName());
        if (a == 1){
            flag = roleService.saveOrUpdate(role);
        }
        return flag;
    }

    //根据id删除
    @DeleteMapping("/delete/{id}")  //通过“/user/”+id 的url调用接口
    public Boolean delete(@PathVariable Integer id) {
        return roleService.removeById(id);
    }
    //批量删除
    @PostMapping("/delete/batch")
    public Boolean deleteBatch(@RequestBody List<Integer> ids){
        return roleService.removeByIds(ids);
    }

    //查询所有
    @GetMapping("/list")
    public Result findAll() {
        return Result.success(roleService.list());
    }

    //根据id查询
    @GetMapping("select/{id}")
    public Role findOne(@PathVariable Integer id) {
        return roleService.getById(id);
    }

    //分页查询
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "") String name) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name); //模糊查询
        return Result.success(roleService.page(new Page<>(pageNum, pageSize) , queryWrapper));
    }

    public int getInfo(String key) {  //查询出一条数据
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", key);
        int a = 2;
        Role one = new Role();
        try {
            one = roleService.getOne(queryWrapper); // 从数据库查询信息
        } catch (Exception e) {
            a = 3;
        }
        if (one == null){
            a = 1;
        }
        return a; //1为不存在数据
        //2为存在一条数据
        //3为存在多条数据
    }


    /**
     * 绑定角色和菜单的关系
     * @param roleId 角色id
     * @param menuIds 菜单id数组
     * @return
     */
    //分配菜单，从路径提取角色id，从body中获得菜单id列表
    @PostMapping("/roleMenu/{roleId}")
    public Result roleMenu(@PathVariable Integer roleId, @RequestBody List<Integer> menuIds) {
        roleService.setRoleMenu(roleId, menuIds);
        return Result.success();
    }

    @GetMapping("/roleMenu/{roleId}")
    public Result getRoleMenu(@PathVariable Integer roleId) {
        return Result.success( roleService.getRoleMenu(roleId));
    }


}

